package com.byxy.controller;

import com.byxy.po.Blog;
import com.byxy.service.BlogService;
import com.byxy.service.CategoryService;
import com.byxy.service.LabelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.byxy.vo.EditorVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
public class BlogController {
    @Resource
    private CategoryService categoryService;
    @Resource
    private LabelService labelService;
    @Resource
    private BlogService blogService;
    @GetMapping("/editor")
    public String editor(Model model) {

       model.addAttribute("categoryList",categoryService.selectAll());
       model.addAttribute("labelList",labelService.selectAll());
        model.addAttribute("editorVo",new EditorVo());

        return "editor";
    }

    @GetMapping("/blog")
    public String blog() {
 /*       //int i=6/0;
        String blog=null;
        if (blog==null){
            throw new NotFoundException("博客不存在");*/
        /*  }*/
        System.out.println("--------------blog--------------");


        return "blog";
    }

    /*  @GetMapping("")
      public String */
    @GetMapping("/details")
    public String details() {
 /*       //int i=6/0;
        String blog=null;
        if (blog==null){
            throw new NotFoundException("博客不存在");*/
        /*  }*/
        System.out.println("--------------Index--------------");


        return "details";

    }
    @PostMapping("/blogs")
    public String blogs(EditorVo editorVo, HttpSession session){
        /*blogService.insert(blog);*/
        if(editorVo!=null){
            Blog blog=new Blog();
            BeanUtils.copyProperties(editorVo,blog);
            blog.setUserId((Integer) session.getAttribute("account"));
           int i= blogService.insertSelective(blog);
            System.out.println(blog.getId());
            return "redirect:/index";
        }else {
            return "redirect:/editor";
        }

    }
    @GetMapping("/myblog")
    public String myblog(){
        return "myblog";
    }
}
